﻿namespace Microsoft.Web.Helpers.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Data.Services;
    using System.Linq;

    public class MockServiceEntities : IUpdatable
    {
        private static List<Product> products = GetProducts();
        private static List<Image> images = GetImages();

        public static List<Product> ProductList
        {
            get { return products; }
        }

        public static List<Image> ImageList
        {
            get { return images; }
        }

        public IQueryable<Product> Products
        {
            get
            {
                return products.AsQueryable<Product>();
            }
        }

        public IQueryable<Image> Images
        {
            get
            {
                return images.AsQueryable<Image>();
            }
        }

        public void AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
        {
            throw new NotImplementedException();
        }

        public void ClearChanges()
        {
            throw new NotImplementedException();
        }

        public object CreateResource(string containerName, string fullTypeName)
        {           
            if (containerName == "Products")
            {
                var product = new Product();
                products.Add(product);
                return product;
            }
            else if (containerName == "Images")
            {
                var image = new Image() { Id = images.Count() + 1 };
                images.Add(image);
                return image;
            }
            else if (fullTypeName == "Microsoft.Web.Helpers.Tests.Supplier")
            {
                return new Supplier();
            }
            else
            {
                return null;
            }
        }

        public void DeleteResource(object targetResource)
        {
            Product productToDelete = targetResource as Product;
            products.Remove(productToDelete);
        }

        public object GetResource(IQueryable query, string fullTypeName)
        {
            object resource = query.Cast<object>().SingleOrDefault();

            // fullTypeName can be null for deletes
            if (fullTypeName != null && resource.GetType().FullName != fullTypeName)
            {
                throw new ApplicationException("Unexpected type for this resource.");
            }

            return resource;
        }

        public object GetValue(object targetResource, string propertyName)
        {
            throw new NotImplementedException();
        }

        public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
        {
            throw new NotImplementedException();
        }

        public object ResetResource(object resource)
        {
            throw new NotImplementedException();
        }

        public object ResolveResource(object resource)
        {
            return resource;
        }

        public void SaveChanges()
        {
        }

        public void SetReference(object targetResource, string propertyName, object propertyValue)
        {
            throw new NotImplementedException();
        }

        public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            targetResource.GetType().GetProperty(propertyName).SetValue(targetResource, propertyValue, null);
        }

        private static List<Product> GetProducts()
        {
            List<Product> products = new List<Product>();
            products.Add(new Product { Id = 1, Name = "Product 1", Rating = 2.5, IsAvailable = true, Priority = 0, Expires = new DateTime(2011, 12, 31), Price = 5.1M, AnualProfits = 201200000L, GlobalId = new Guid("12345678-aaaa-bbbb-cccc-ddddeeeeffff"), Category = 1, Cost = 1.5f, Status = 5, Supplier = new Supplier() { Name = "Provider 1", Address = "Test Address" } });
            products.Add(new Product { Id = 2, Name = "Product 2", Rating = 1.5, IsAvailable = true, Priority = 1, Expires = new DateTime(2011, 12, 31), Price = 3.5M, AnualProfits = 100000000L, Category = 2 });
            products.Add(new Product { Id = 3, Name = "Product 3", Rating = 3.0, IsAvailable = false, Priority = 2, Expires = new DateTime(2011, 12, 31), Price = 2.0M, AnualProfits = 200100000L, Category = 3 });

            return products;
        }

        private static List<Image> GetImages()
        {
            List<Image> images = new List<Image>();
            images.Add(new Image { Id = 1, Name = "Image 1" });
            images.Add(new Image { Id = 2, Name = "Image 2" });
            images.Add(new Image { Id = 3, Name = "Image 3" });

            return images;
        }
    }
}
